package com.michaelbulava.TFS.ui.controls.workspaces;

import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.versioncontrol.*;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ServerSettings;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.jni.helpers.LocalHost;
import com.microsoft.tfs.util.Check;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by mbulava on 7/15/2015.
 */
public class WorkspaceDetails
{
    private String name;
    private String server;
    private String owner;
    private String computer;
    private String comment;
    private WorkspaceLocation workspaceLocation = WorkspaceLocation.SERVER;
    private final boolean workspaceLocationReadOnly;
    private WorkspaceOptions workspaceOptions = WorkspaceOptions.NONE;
    private final boolean workspaceOptionsReadOnly;
    private WorkspacePermissionProfile workspacePermissionProfile = WorkspacePermissionProfile.getPrivateProfile();
    private final boolean workspacePermissionProfileReadOnly;
    private boolean allowAdminister;

    private WorkspaceDetails(VersionControlClient client)
    {
        Check.notNull(client, "client");

        ServerSettings serverSettings = client.getServerSettingsWithFallback(new AtomicBoolean());

        setWorkspaceLocation(serverSettings.getDefaultWorkspaceLocation());
        setPermissionProfile(WorkspacePermissionProfile.getPrivateProfile());


        this.workspacePermissionProfileReadOnly = (!client.getServerSupportedFeatures().contains(SupportedFeatures.WORKSPACE_PERMISSIONS));



        boolean is2012OrLater = client.getServiceLevel().getValue() >= WebServiceLevel.TFS_2012.getValue();
        this.workspaceLocationReadOnly = (!is2012OrLater);
        this.workspaceOptionsReadOnly = (!is2012OrLater);
    }

    public WorkspaceDetails(TFSTeamProjectCollection connection, String defaultWorkspaceName)
    {
        this(connection != null ? connection.getVersionControlClient() : null);

        this.allowAdminister = true;
        this.name = defaultWorkspaceName;
        this.workspacePermissionProfile = WorkspacePermissionProfile.getPrivateProfile();




        this.server = connection.getBaseURI().getHost();






        this.computer = LocalHost.getShortName();
        if (connection.getVersionControlClient().getServiceLevel().getValue() >= WebServiceLevel.TFS_2012.getValue()) {
            this.owner = connection.getAuthorizedIdentity().getDisplayName();
        } else {
            this.owner = connection.getAuthorizedIdentity().getUniqueName();
        }
    }

    public WorkspaceDetails(Workspace workspace)
    {
        this(workspace != null ? workspace.getClient() : null);

        this.name = workspace.getName();
        this.server = workspace.getClient().getConnection().getBaseURI().getHost();
        this.owner = workspace.getOwnerDisplayName();
        this.computer = workspace.getComputer();
        this.comment = workspace.getComment();


        this.workspacePermissionProfile = workspace.getPermissionsProfile();

        this.allowAdminister = workspace.hasAdministerPermission();


        this.workspaceLocation = workspace.getLocation();
        this.workspaceOptions = workspace.getOptions();
        if ((this.comment != null) && (this.comment.trim().length() == 0)) {
            this.comment = null;
        }
    }

    public String getName()
    {
        return this.name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getServer()
    {
        return this.server;
    }

    public String getOwner()
    {
        return this.owner;
    }

    public String getComputer()
    {
        return this.computer;
    }

    public String getComment()
    {
        return this.comment;
    }

    public void setComment(String comment)
    {
        this.comment = comment;
    }

    public WorkspaceLocation getWorkspaceLocation()
    {
        return this.workspaceLocation;
    }

    public void setWorkspaceLocation(WorkspaceLocation workspaceLocation)
    {
        this.workspaceLocation = workspaceLocation;
    }

    public boolean isWorkspaceLocationReadOnly()
    {
        return this.workspaceLocationReadOnly;
    }

    public WorkspacePermissionProfile getPermissionProfile()
    {
        return this.workspacePermissionProfile;
    }

    public void setPermissionProfile(WorkspacePermissionProfile workspacePermissionProfile)
    {
        this.workspacePermissionProfile = workspacePermissionProfile;
    }

    public boolean isWorkspacePermissionProfileReadOnly()
    {
        return this.workspacePermissionProfileReadOnly;
    }

    public WorkspaceOptions getWorkspaceOptions()
    {
        return this.workspaceOptions;
    }

    public void setWorkspaceOptions(WorkspaceOptions workspaceOptions)
    {
        this.workspaceOptions = workspaceOptions;
    }

    public boolean isWorkspaceOptionsReadOnly()
    {
        return this.workspaceOptionsReadOnly;
    }

    public boolean isAdministerAllowed()
    {
        return this.allowAdminister;
    }
}

